package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    public PageResponse<NursingProjectVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<List<NursingProject>> page = nursingProjectMapper.selectByPage(name, status);
        return PageResponse.of(page, NursingProjectVo.class);
    }

    @Override
    public void add(NursingProjectDto dto) {
        if (nursingProjectMapper.selectByName(dto.getName()) != null){
            throw new BaseException(BasicEnum.NURSING_PROJECT_EXIST);
        }
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(dto, nursingProject);
        nursingProjectMapper.insert(nursingProject);
    }

    @Override
    public NursingProjectVo searchById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        NursingProjectVo nursingProjectVo = new NursingProjectVo();
        BeanUtils.copyProperties(nursingProject, nursingProjectVo);
        return nursingProjectVo;
    }

    @Override
    public void modifyNursingProject(NursingProjectDto dto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(dto, nursingProject);
        int count =  nursingProjectPlanMapper.selectByNursingProjectId(nursingProject.getId());
        if (count != 0){
            throw new BaseException(BasicEnum.NURSING_PROJECT_PLAN_EXIST);
        }
        if (nursingProjectMapper.selectDiffByName(dto.getName()) != null){
            throw new BaseException(BasicEnum.NURSING_PROJECT_EXIST);
        }
        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    @Override
    public void modifyNursingProjectStatus(Long id, Integer status) {
        int count =  nursingProjectPlanMapper.selectByNursingProjectId(id);
        if (count != 0){
            throw new BaseException(BasicEnum.NURSING_PROJECT_PLAN_EXIST);
        }
        nursingProjectMapper.updateNursingProjectStatus(id, status);
    }

    @Override
    public void deleteById(Long id) {
        int count =  nursingProjectPlanMapper.selectByNursingProjectId(id);
        if (count != 0){
            throw new BaseException(BasicEnum.NURSING_PROJECT_PLAN_EXIST);
        }
        nursingProjectMapper.delectById(id);
    }

    @Override
    public List<NursingProjectVo> findAll() {
        List<NursingProjectVo> nursingProjectVoList = nursingProjectMapper.findAll();
        return nursingProjectVoList;
    }
}
